package com.whity.towerdefense.controler.states;

import com.whity.towerdefense.Utils;
import com.whity.towerdefense.controler.GameControler;
import com.whity.towerdefense.controler.I_State;
import com.whity.towerdefense.model.GameModel;
import com.whity.towerdefense.view.GraphicElt;

import android.graphics.PointF;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class ScaleAP implements I_State
{
	// =============================================================
	// Private attributs :
	
	@SuppressWarnings("unused")
	private static final String TAG = "com.djm.test03.controler.states";
	
	private float 			m_LastDistance;
	private PointF			m_Barycenter; // Coordonee du point situer entre les 2 doigt de l'utilisateur quand la manip commence
	private GraphicElt		m_MapGraphicElt;
	private final float		m_PropX;
	private final float		m_PropY;
	
	// =============================================================
	// Private functions :
	
	private float distance(PointF p_Pt1, PointF p_Pt2)
	{
		return (float) Math.sqrt((p_Pt1.x-p_Pt2.x)*(p_Pt1.x-p_Pt2.x) + (p_Pt1.y-p_Pt2.y)*(p_Pt1.y-p_Pt2.y));
	}
	
	private float distance(float p_X1, float p_Y1, float p_X2, float p_Y2)
	{
		return (float) Math.sqrt((p_X1-p_X2)*(p_X1-p_X2) + (p_Y1-p_Y2)*(p_Y1-p_Y2));
	}
	
	// =============================================================
	// Public functions :
	
	public ScaleAP(PointF p_Pt1, PointF p_Pt2)
	{
		m_LastDistance = distance(p_Pt1, p_Pt2);
		m_Barycenter = new PointF((p_Pt1.x + p_Pt2.x)/2.0f, (p_Pt1.y + p_Pt2.y)/2.0f);
		m_MapGraphicElt = GameModel.LEVEL.GetMap().GetGraphic();
		
		PointF baryMap = new PointF(m_Barycenter.x, m_Barycenter.y);
		Utils.ScreenToMap(baryMap);
		
		m_PropX = baryMap.x/GameModel.LEVEL.GetMap().GetWidthInCells();
		m_PropY = baryMap.y/GameModel.LEVEL.GetMap().GetHeightInCells();
		
		baryMap.x /= m_MapGraphicElt.GetWidth();
		baryMap.y /= m_MapGraphicElt.GetHeight();
	}
	
	public void OnChangeState()
	{
		
	}

	public void OnClickEvent(View p_View)
	{
		
	}
	
	public void OnDrawerOpenEvent()
	{
		
	}
	
	public void OnDrawerCloseEvent()
	{
		
	}
	
	public void OnPause()
	{
		
	}
	
	public void OnResume()
	{
		
	}
	
	public boolean OnMotionEvent(View p_View, MotionEvent p_Evt)
	{
		if(p_Evt.getAction() == MotionEvent.ACTION_MOVE)
		{
			// On effectue le scaling autour du barycentre
			float dist = distance(p_Evt.getX(0), p_Evt.getY(0), p_Evt.getX(1), p_Evt.getY(1));
			float factor = dist / m_LastDistance;
			PointF pos = m_MapGraphicElt.GetPosition();
			m_LastDistance = dist;
			PointF baryFingers = new PointF((p_Evt.getX(0) + p_Evt.getX(1))/2.0f, (p_Evt.getY(0) + p_Evt.getY(1))/2.0f);
			
			PointF baryPos = new PointF(m_Barycenter.x, m_Barycenter.y);
			Utils.ScreenToMap(baryPos);
			baryPos.x /= m_MapGraphicElt.GetWidth();
			baryPos.y /= m_MapGraphicElt.GetHeight();
			
			float oldScale = m_MapGraphicElt.GetScale();
			float newScale = oldScale * factor;
			float w = m_MapGraphicElt.GetWidth();
			float h = m_MapGraphicElt.GetHeight();
			
			m_MapGraphicElt.SetPosition(pos.x + (baryFingers.x - m_Barycenter.x)/Utils.RESOLUTION_X*Utils.SCREEN_RATIO, pos.y + (m_Barycenter.y - baryFingers.y)/Utils.RESOLUTION_Y);
			m_MapGraphicElt.SetScale(m_MapGraphicElt.GetScale() * factor);
			
			float deltaScaleX = (w*oldScale-w*newScale);
			float deltaScaleY = (h*oldScale-h*newScale);
			
			//m_MapGraphicElt.SetPosition(pos.x + deltaScaleX*m_PropX , pos.y + deltaScaleY*m_PropY);
			m_MapGraphicElt.SetPosition(pos.x + deltaScaleX*m_PropX , pos.y + deltaScaleY*m_PropY);
	
			
			
			
			
			
			m_Barycenter = baryFingers;
			return true;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_POINTER_2_UP)
		{
			GameControler.SetState(new DragMapAP(p_Evt.getX(), p_Evt.getY()));
			return true;
		}
		else if(p_Evt.getAction() == MotionEvent.ACTION_POINTER_1_UP)
		{
			GameControler.SetState(new DragMapAP(p_Evt.getX(1), p_Evt.getY(1)));
			return true;
		}
		else
		{
			return false;
		}
	}
	
	// =============================================================

}
